package com.css.zfzx.sjcj.common.utils;

import com.css.zfzx.sjcj.common.vo.DivisionVo;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class ProvinceCityAreaUtil {
    private static final List<DivisionVo> pcaList = new ArrayList<>();

    /**
     * 将所有省市县放在同一对象中
     *
     * @return
     */
    public static void initProvinceCityArea(List<DivisionVo> divisionlist) {
        //查询数据库
        for (DivisionVo division : divisionlist) {
            DivisionVo divisionVo = new DivisionVo();
            divisionVo.setProvinceCode(division.getProvinceCode());
            String[] split = division.getAllParentId().split(",");
            if (split.length == 2) {
                //省
                divisionVo.setProvinceName(division.getProvinceName());
            } else if (split.length == 3) {
                //市
                divisionVo.setProvinceName(getCityName(split[1], divisionlist));
                divisionVo.setCityName(division.getProvinceName());
            } else if (split.length == 4) {
                //县
                divisionVo.setProvinceName(getCityName(split[1], divisionlist));
                divisionVo.setCityName(getCityName(split[2], divisionlist));
                divisionVo.setAreaName(division.getProvinceName());
            }
            pcaList.add(divisionVo);
        }
    }
    public static String getCityName(String id, List<DivisionVo> list) {
        for (DivisionVo divisionEntity : list) {//id ==
            if (id.equals(divisionEntity.getDivisionId())) {
                return divisionEntity.getProvinceName();
            }
        }
        return null;
    }

    public static String getCode(String provinceName, String cityName, String areaName) {
        String code = null;
        for (DivisionVo pca : pcaList) {
            // 四个直辖市只有两级需要单独处理
            if (provinceName.contains("北京") || provinceName.contains("重庆") || provinceName.contains("上海") || provinceName.contains("天津")) {
                if (StringUtils.isEmpty(areaName)||"null".equals(areaName)) {
                    if (provinceName.equals(pca.getProvinceName())) {
                        code= pca.getProvinceCode();
                        break;
                    }
                } else {
                    if (provinceName.equals(pca.getProvinceName()) && areaName.equals(pca.getCityName())) {
                        code= pca.getProvinceCode();
                        break;
                    }
                }
            } else {//非直辖市的都为三级
                if (StringUtils.isEmpty(cityName)||"null".equals(cityName)) {
                    if (provinceName.equals(pca.getProvinceName())) {
                        code = pca.getProvinceCode();
                        break;
                    }
                }
                if (StringUtils.isNotEmpty(cityName)&&!"null".equals(cityName)&&("null".equals(areaName) || StringUtils.isEmpty(areaName))) {
                    if (provinceName.equals(pca.getProvinceName()) && cityName.equals(pca.getCityName())) {
                        code = pca.getProvinceCode();
                        break;
                    }
                }
                if (StringUtils.isNotEmpty(areaName)&&!"null".equals(areaName)) {
                    if (provinceName.equals(pca.getProvinceName()) && cityName.equals(pca.getCityName()) && areaName.equals(pca.getAreaName())) {
                        code = pca.getProvinceCode();
                        break;
                    }
                }
            }
        }
        return code;
    }
}
